using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using DevExpress.LookAndFeel;
using DevExpress.Skins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Docking2010.Views;
using DevExpress.XtraBars.Helpers;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraEditors;
using DevExpress.XtraNavBar;
using CTEM.Models;
using CTEM.Controllers.BUS;
using CTEM.Controllers.Global;
using CTEM.ModuleForms.ManageDebt;
using CTEM.ModuleForms.ManageEmployee;
using CTEM.ModuleForms.ManageVehicle;
using CTEM.ModuleForms.ManageWarehouse;
using CTEM.ModuleForms.ManageLedger;
using CTEM.ModuleForms.CommonForm;
using CTEM.ModuleForms.ManageConstruction;
using CTEM.ModuleForms.ManageWorker;
using CTEM.ModuleForms.ManageReport;
using CTEM.ModuleForms.ManagePurchaseBook;

namespace CTEM {
    public partial class MainForm : RibbonForm {
        public static Size HoverSkinImageSize = new Size(116, 86);
        public static Size SkinImageSize = new Size(58, 43);
        private List<LeftMenuDTO> listMenus;
        public MainForm() {
            this.IsMdiContainer = true;
            InitializeComponent();
            mainRibbon.MdiMergeStyle = RibbonMdiMergeStyle.Always;
            InitSkins();
            InitMenu();
            tabbedView.DocumentAdded += tabbedView_DocumentAdded;
            tabbedView.DocumentRemoved += tabbedView_DocumentRemoved;
            tabbedView.FloatDocuments.CollectionChanged += tabbedView_FloatDocumentsCollectionChanged;
        }

        private void InitMenu()
        {
            nbcMenu.SelectedLink = null;
            foreach (NavBarGroup group in nbcMenu.Groups)
            {
                for (int i = group.ItemLinks.Count - 1; i >= 0; i--)
                {
                    nbcMenu.Items.Remove(group.ItemLinks[i].Item);
                }
            }
            LeftMenuBUS leftMenuBUS = new LeftMenuBUS();
            listMenus = leftMenuBUS.GetMenuByRoleID(Global.CurrentUser.RoleID);
            foreach (var menu in listMenus)
            {
                if (menu.Type.Equals(Constants.CONSTRUCTION))
                {
                    NavBarItem item = CreateMenuItem(menu);
                    nbcMenu.Items.Add(item);
                    constructionGroup.ItemLinks.Add(item);
                }
                if (menu.Type.Equals(Constants.EMPLOYEE))
                {
                    NavBarItem item = CreateMenuItem(menu);
                    nbcMenu.Items.Add(item);
                    employeeGroup.ItemLinks.Add(item);
                }
                if (menu.Type.Equals(Constants.WAREHOUSE))
                {
                    NavBarItem item = CreateMenuItem(menu);
                    nbcMenu.Items.Add(item);
                    warehouseGroup.ItemLinks.Add(item);
                }
                if (menu.Type.Equals(Constants.DEBT))
                {
                    NavBarItem item = CreateMenuItem(menu);
                    nbcMenu.Items.Add(item);
                    debtGroup.ItemLinks.Add(item);
                }
                if (menu.Type.Equals(Constants.LEDGER))
                {
                    NavBarItem item = CreateMenuItem(menu);
                    nbcMenu.Items.Add(item);
                    ledgerGroup.ItemLinks.Add(item);
                }
                if (menu.Type.Equals(Constants.SYSTEM))
                {
                    NavBarItem item = CreateMenuItem(menu);
                    nbcMenu.Items.Add(item);
                    systemGroup.ItemLinks.Add(item);
                }
                if (menu.Type.Equals(Constants.REPORT))
                {
                    NavBarItem item = CreateMenuItem(menu);
                    nbcMenu.Items.Add(item);
                    systemGroup.ItemLinks.Add(item);
                }
            }

        }

        private NavBarItem CreateMenuItem(LeftMenuDTO menu)
        {
            using (NavBarItem item = new NavBarItem() { Caption = menu.MenuName, Hint = menu.Description, Tag = menu })
            {
                item.LinkClicked += OnMenuLinkClicked;
                return item;
            }
        }

        void OnMenuLinkClicked(object sender, NavBarLinkEventArgs e)
        {
            if (e.Link.Caption.Equals(Constants.MANAGE_ROLE))
            {
                RoleManagement rolemanagement = new RoleManagement();
                BaseDocument document = null;
                if (!Documents.TryGetValue(rolemanagement.Name, out document))
                {
                    rolemanagement.MdiParent = this;
                    rolemanagement.Show();
                    if (tabbedView.Documents.TryGetValue(rolemanagement, out document))
                    {
                        document.Caption = e.Link.Caption;
                    }
                }
                else tabbedView.Controller.Activate(document);
            }
            else if (e.Link.Caption.Equals(Constants.MANAGE_USER))
            {
                EmployeeManagement employeemanagement = new EmployeeManagement();
                BaseDocument document = null;
                if (!Documents.TryGetValue(employeemanagement.Name, out document))
                {
                    employeemanagement.MdiParent = this;
                    employeemanagement.Show();
                    if (tabbedView.Documents.TryGetValue(employeemanagement, out document))
                    {
                        document.Caption = e.Link.Caption;
                    }
                }
                else tabbedView.Controller.Activate(document);
            }
            else if (e.Link.Caption.Equals(Constants.MANAGE_DEBT))
            {
                SupplierManagement debtmanagement = new SupplierManagement();
                BaseDocument document = null;
                if (!Documents.TryGetValue(debtmanagement.Name, out document))
                {
                    debtmanagement.MdiParent = this;
                    debtmanagement.Show();
                    if (tabbedView.Documents.TryGetValue(debtmanagement, out document))
                    {
                        document.Caption = e.Link.Caption;
                    }
                }
                else tabbedView.Controller.Activate(document);
            }
            else if (e.Link.Caption.Equals(Constants.MANAGE_EMPLOYEE_SALARY))
            {
                EmployeeExpenseReceipts employeeexpensereceipts = new EmployeeExpenseReceipts();
                BaseDocument document = null;
                if (!Documents.TryGetValue(employeeexpensereceipts.Name, out document))
                {
                    employeeexpensereceipts.MdiParent = this;
                    employeeexpensereceipts.Show();
                    if (tabbedView.Documents.TryGetValue(employeeexpensereceipts, out document))
                    {
                        document.Caption = e.Link.Caption;
                    }
                }
                else tabbedView.Controller.Activate(document);
            }
            else if (e.Link.Caption.Equals(Constants.MANAGE_MATERIAL))
            {
                MaterialManagement materialmanagement = new MaterialManagement();
                BaseDocument document = null;
                if (!Documents.TryGetValue(materialmanagement.Name, out document))
                {
                    materialmanagement.MdiParent = this;
                    materialmanagement.Show();
                    if (tabbedView.Documents.TryGetValue(materialmanagement, out document))
                    {
                        document.Caption = e.Link.Caption;
                    }
                }
                else tabbedView.Controller.Activate(document);
            }
            else if (e.Link.Caption.Equals(Constants.MANAGE_VEHICLE))
            {
                VehicleManagement vehiclemanagement = new VehicleManagement();
                BaseDocument document = null;
                if (!Documents.TryGetValue(vehiclemanagement.Name, out document))
                {
                    vehiclemanagement.MdiParent = this;
                    vehiclemanagement.Show();
                    if (tabbedView.Documents.TryGetValue(vehiclemanagement, out document))
                    {
                        document.Caption = e.Link.Caption;
                    }
                }
                else tabbedView.Controller.Activate(document);
            }
            else if (e.Link.Caption.Equals(Constants.MANAGE_CONSTRUCTION))
            {
                ConstructionManagement constructionManagement = new ConstructionManagement();
                BaseDocument document = null;
                if (!Documents.TryGetValue(constructionManagement.Name, out document))
                {
                    constructionManagement.MdiParent = this;
                    constructionManagement.Show();
                    if (tabbedView.Documents.TryGetValue(constructionManagement, out document))
                    {
                        document.Caption = e.Link.Caption;
                    }
                }
                else tabbedView.Controller.Activate(document);
            }
            else if (e.Link.Caption.Equals(Constants.MANAGE_ESTIMATE))
            {
                EstimateManagement estimateManagement = new EstimateManagement();
                BaseDocument document = null;
                if (!Documents.TryGetValue(estimateManagement.Name, out document))
                {
                    estimateManagement.MdiParent = this;
                    estimateManagement.Show();
                    if (tabbedView.Documents.TryGetValue(estimateManagement, out document))
                    {
                        document.Caption = e.Link.Caption;
                    }
                }
                else tabbedView.Controller.Activate(document);
            }
            else if (e.Link.Caption.Equals(Constants.MANAGE_WAREHOUSE))
            {
                WarehouseManagement warehouseManagement = new WarehouseManagement();
                BaseDocument document = null;
                if (!Documents.TryGetValue(warehouseManagement.Name, out document))
                {
                    warehouseManagement.MdiParent = this;
                    warehouseManagement.Show();
                    if (tabbedView.Documents.TryGetValue(warehouseManagement, out document))
                    {
                        document.Caption = e.Link.Caption;
                    }
                }
                else tabbedView.Controller.Activate(document);
            }
            else if (e.Link.Caption.Equals(Constants.MANAGE_SUBCONTRACTORS))
            {
                //SubcontractorManagement subcontractorManagement = new SubcontractorManagement();
                SubcontractorContractManagement subcontractorManagement = new SubcontractorContractManagement();
                BaseDocument document = null;
                if (!Documents.TryGetValue(subcontractorManagement.Name, out document))
                {
                    subcontractorManagement.MdiParent = this;
                    subcontractorManagement.Show();
                    if (tabbedView.Documents.TryGetValue(subcontractorManagement, out document))
                    {
                        document.Caption = e.Link.Caption;
                    }
                }
                else tabbedView.Controller.Activate(document);
            }
            else if (e.Link.Caption.Equals(Constants.MANAGE_LEDGER))
            {
                LedgerManagement ledgerManagement = new LedgerManagement();
                BaseDocument document = null;
                if (!Documents.TryGetValue(ledgerManagement.Name, out document))
                {
                    ledgerManagement.MdiParent = this;
                    ledgerManagement.Show();
                    if (tabbedView.Documents.TryGetValue(ledgerManagement, out document))
                    {
                        document.Caption = e.Link.Caption;
                    }
                }
                else tabbedView.Controller.Activate(document);
            }
            else if (e.Link.Caption.Equals(Constants.MANAGE_ACCOUNT))
            {
                PurchaseBookManagement purchaseBookManagement = new PurchaseBookManagement();
                BaseDocument document = null;
                if (!Documents.TryGetValue(purchaseBookManagement.Name, out document))
                {
                    purchaseBookManagement.MdiParent = this;
                    purchaseBookManagement.Show();
                    if (tabbedView.Documents.TryGetValue(purchaseBookManagement, out document))
                    {
                        document.Caption = e.Link.Caption;
                    }
                }
                else tabbedView.Controller.Activate(document);
            }
            else if (e.Link.Caption.Equals(Constants.MANAGE_WORKER))
            {
                WorkerSalaryManagement workerSalaryManagement = new WorkerSalaryManagement();
                BaseDocument document = null;
                if (!Documents.TryGetValue(workerSalaryManagement.Name, out document))
                {
                    workerSalaryManagement.MdiParent = this;
                    workerSalaryManagement.Show();
                    if (tabbedView.Documents.TryGetValue(workerSalaryManagement, out document))
                    {
                        document.Caption = e.Link.Caption;
                    }
                }
                else tabbedView.Controller.Activate(document);
                
            }
                
            else if (e.Link.Caption.Equals(Constants.MANAGE_LEDGER_DETAIL))
            {
                LedgerManagement ledgerManagement = new LedgerManagement();
                BaseDocument document = null;
                if (!Documents.TryGetValue(ledgerManagement.Name, out document))
                {
                    ledgerManagement.MdiParent = this;
                    ledgerManagement.Show();
                    if (tabbedView.Documents.TryGetValue(ledgerManagement, out document))
                    {
                        document.Caption = e.Link.Caption;
                    }
                }
                else tabbedView.Controller.Activate(document);
            }
            else if (e.Link.Caption.Equals(Constants.REPORT_WORKERSALARY))
            {
                WorkerSalaryReport workerSalaryReport = new WorkerSalaryReport();
                BaseDocument document = null;
                if (!Documents.TryGetValue(workerSalaryReport.Name, out document))
                {
                    workerSalaryReport.MdiParent = this;
                    workerSalaryReport.Show();
                    if (tabbedView.Documents.TryGetValue(workerSalaryReport, out document))
                    {
                        document.Caption = e.Link.Caption;
                    }
                }
                else tabbedView.Controller.Activate(document);
            }

            else if (e.Link.Caption.Equals(Constants.REPORT_EMPLOYEESALARY))
            {
                EmployeeSalaryReportDetail employeeSalaryReport = new EmployeeSalaryReportDetail();
                BaseDocument document = null;
                if (!Documents.TryGetValue(employeeSalaryReport.Name, out document))
                {
                    employeeSalaryReport.MdiParent = this;
                    employeeSalaryReport.Show();
                    if (tabbedView.Documents.TryGetValue(employeeSalaryReport, out document))
                    {
                        document.Caption = e.Link.Caption;
                    }
                }
                else tabbedView.Controller.Activate(document);
            }

            else if (e.Link.Caption.Equals(Constants.REPORT_LEDGER))
            {
                LedgerReport ledgerReport = new LedgerReport();
                BaseDocument document = null;
                if (!Documents.TryGetValue(ledgerReport.Name, out document))
                {
                    ledgerReport.MdiParent = this;
                    ledgerReport.Show();
                    if (tabbedView.Documents.TryGetValue(ledgerReport, out document))
                    {
                        document.Caption = e.Link.Caption;
                    }
                }
                else tabbedView.Controller.Activate(document);
            }
        }
        
        void InitSkins() {
            SkinHelper.InitSkinGallery(skinGalleryBarItem, true);
            UserLookAndFeel.Default.SetSkinStyle("DevExpress Style");
        }
        
        IDictionary<object, BaseDocument> documentsCore = new Dictionary<object, BaseDocument>();
        public IDictionary<object, BaseDocument> Documents {
            get { return documentsCore; }
        }

        void tabbedView_DocumentAdded(object sender, DocumentEventArgs e)
        {
            RegisterDocument(e.Document);
        }
        void tabbedView_DocumentRemoved(object sender, DocumentEventArgs e)
        {
            UnregisterDocument(e.Document);
        }

        void RegisterDocument(BaseDocument document)
        {
            RoleManagement rolemanagement = document.Form as RoleManagement;            
            if (rolemanagement != null) Documents.Add(rolemanagement.Name, document);
            SupplierManagement debtmanagement = document.Form as SupplierManagement;
            if (debtmanagement != null) Documents.Add(debtmanagement.Name, document);
            EmployeeManagement employeemanagement = document.Form as EmployeeManagement;
            if (employeemanagement != null) Documents.Add(employeemanagement.Name, document);
            EmployeeExpenseReceipts employeeexpensereceipts = document.Form as EmployeeExpenseReceipts;
            if (employeeexpensereceipts != null) Documents.Add(employeeexpensereceipts.Name, document);
            MaterialManagement materialmanagement = document.Form as MaterialManagement;
            if (materialmanagement != null) Documents.Add(materialmanagement.Name, document);
            VehicleManagement vehiclemanagement = document.Form as VehicleManagement;
            if (vehiclemanagement != null) Documents.Add(vehiclemanagement.Name, document);
            ConstructionManagement constructionManagement = document.Form as ConstructionManagement;
            if (constructionManagement != null) Documents.Add(constructionManagement.Name, document);
            EstimateManagement estimateManagement = document.Form as EstimateManagement;
            if (estimateManagement != null) Documents.Add(estimateManagement.Name, document);
            WarehouseManagement warehouseManagement = document.Form as WarehouseManagement;
            if (warehouseManagement != null) Documents.Add(warehouseManagement.Name, document);
            SubcontractorManagement subcontractorManagement = document.Form as SubcontractorManagement;
            if (subcontractorManagement != null) Documents.Add(subcontractorManagement.Name, document);
            PurchaseBookManagement purchaseBookManagement = document.Form as PurchaseBookManagement;
            if (purchaseBookManagement != null) Documents.Add(purchaseBookManagement.Name, document);
            WorkerSalaryManagement workerSalaryManagement = document.Form as WorkerSalaryManagement;
            if (workerSalaryManagement != null) Documents.Add(workerSalaryManagement.Name, document);
            LedgerManagement ledgerManagement = document.Form as LedgerManagement;
            if (ledgerManagement != null) Documents.Add(ledgerManagement.Name, document);
            WorkerSalaryReport workerSalaryReport = document.Form as WorkerSalaryReport;
            if (workerSalaryReport != null) Documents.Add(workerSalaryReport.Name, document);
            EmployeeSalaryReportDetail employeeSalaryReport = document.Form as EmployeeSalaryReportDetail;
            if (employeeSalaryReport != null) Documents.Add(employeeSalaryReport.Name, document);
            LedgerReport ledgerReport = document.Form as LedgerReport;
            if (ledgerReport != null) Documents.Add(ledgerReport.Name, document);
        }
        void UnregisterDocument(BaseDocument document)
        {
            RoleManagement rolemanagement = document.Form as RoleManagement;
            if (rolemanagement != null) Documents.Remove(rolemanagement.Name);
            SupplierManagement debtmanagement = document.Form as SupplierManagement;
            if (debtmanagement != null) Documents.Remove(debtmanagement.Name);
            EmployeeManagement employeemanagement = document.Form as EmployeeManagement;
            if (employeemanagement != null) Documents.Remove(employeemanagement.Name);
            EmployeeExpenseReceipts employeeexpensereceipts = document.Form as EmployeeExpenseReceipts;
            if (employeeexpensereceipts != null) Documents.Remove(employeeexpensereceipts.Name);
            MaterialManagement materialmanagement = document.Form as MaterialManagement;
            if (materialmanagement != null) Documents.Remove(materialmanagement.Name);
            VehicleManagement vehiclemanagement = document.Form as VehicleManagement;
            if (vehiclemanagement != null) Documents.Remove(vehiclemanagement.Name);
            ConstructionManagement constructionManagement = document.Form as ConstructionManagement;
            if (constructionManagement != null) Documents.Remove(constructionManagement.Name);
            EstimateManagement estimateManagement = document.Form as EstimateManagement;
            if (estimateManagement != null) Documents.Remove(estimateManagement.Name);
            WarehouseManagement warehouseManagement = document.Form as WarehouseManagement;
            if (warehouseManagement != null) Documents.Remove(warehouseManagement.Name);
            SubcontractorManagement subcontractorManagement = document.Form as SubcontractorManagement;
            if (subcontractorManagement != null) Documents.Remove(subcontractorManagement.Name);
            PurchaseBookManagement purchaseBookManagement = document.Form as PurchaseBookManagement;
            if (purchaseBookManagement != null) Documents.Remove(purchaseBookManagement.Name);
            WorkerSalaryManagement workerSalaryManagement = document.Form as WorkerSalaryManagement;
            if (workerSalaryManagement != null) Documents.Remove(workerSalaryManagement.Name);
            LedgerManagement ledgerManagement = document.Form as LedgerManagement;
            if (ledgerManagement != null) Documents.Remove(ledgerManagement.Name);
            WorkerSalaryReport workerSalaryReport = document.Form as WorkerSalaryReport;
            if (workerSalaryReport != null) Documents.Remove(workerSalaryReport.Name);
            EmployeeSalaryReportDetail employeeSalaryReport = document.Form as EmployeeSalaryReportDetail;
            if (employeeSalaryReport != null) Documents.Remove(employeeSalaryReport.Name);
            LedgerReport ledgerReport = document.Form as LedgerReport;
            if (ledgerReport != null) Documents.Remove(ledgerReport.Name);
        }
        void tabbedView_FloatDocumentsCollectionChanged(DevExpress.XtraBars.Docking2010.Base.CollectionChangedEventArgs<BaseDocument> ea)
        {
            if (ea.ChangedType == DevExpress.XtraBars.Docking2010.Base.CollectionChangedType.ElementAdded)
                RegisterDocument(ea.Element);
            if (ea.ChangedType == DevExpress.XtraBars.Docking2010.Base.CollectionChangedType.ElementRemoved)
                UnregisterDocument(ea.Element);
        }

        private void MainForm_Load(object sender, EventArgs e)
        {

        }

        private void mainRibbon_ApplicationButtonClick(object sender, EventArgs e)
        {
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void LogoutButton_Click(object sender, EventArgs e)
        {
            LoginForm.IsLogout = true;
            Close();
        }

        private void bbiProfile_ItemClick(object sender, ItemClickEventArgs e)
        {
            using (Profiles profilesform = new Profiles(Global.CurrentUser))
            {
                profilesform.ShowDialog();
            }
        }
    }
}